//
//  XDPersonCollectViewController.m
//  Le way walkers
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPersonCollectViewController.h"
#import "XDRecommendDateBaseManager.h"
#import "XDRecommend.h"
#import "XDDetailsViewController.h"
#import "XDPersonTableViewCell.h"
@interface XDPersonCollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *Arr;
@property (nonatomic, retain) NSDictionary *dic;
@property(nonatomic,retain)XDRecommendDateBaseManager *dbManager;
@property (nonatomic, retain) UILabel *null_text;
@end

@implementation XDPersonCollectViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的收藏";
    [self createTableView];
    
    self.null_text = [[UILabel alloc] init];
    [self.null_text setText:@"当前没有任何收藏哦~"];
    [self.null_text setTextColor:[UIColor colorWithRed:50 / 255.0 green:179 / 255.0 blue:255 / 255.0 alpha:1]];
    [self.null_text setFrame:CGRectMake(self.view.frame.size.width / 2.0 - 100, 50, 200, 30)];
//    [null_text setBackgroundColor:[UIColor yellowColor]];
    self.null_text.hidden = YES;
    [self.view addSubview:self.null_text];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    //创建数据库单例对象
    self.dbManager = [XDRecommendDateBaseManager shareInstance];
    self.Arr = [_dbManager selectInfo];
    NSLog(@"///%@", self.Arr);
    
    if (self.Arr.count == 0) {
        [self null_data];
        self.null_text.hidden = NO;
    }
    else
    {
        self.null_text.hidden = YES;
    }
    
    //设置编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)null_data
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前没有任何收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [av show];
    [av release];
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.tableView reloadData];
//    NSLog(@"789");
//}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

//开始编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //调用父类的编辑方法
    [super setEditing:editing animated:animated];
    //利用系统的编辑按钮控制tableView编辑状态
    [_tableView setEditing:editing animated:animated];
}



//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



//判断编辑状态进行删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        self.dbManager = [XDRecommendDateBaseManager shareInstance];
        [_dbManager deleteInfoWithXDReconmmendName:[_Arr[indexPath.row] name]];
        //删除cell之前 先从数组中对应位置 删除数据
        [_Arr removeObjectAtIndex:indexPath.row];
        //删除的信息需要被存放在数组中
        NSArray *arr = @[indexPath];
        //让视图和数据匹配
        [_tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationLeft];
    }
}




//删除样式
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}




-(void)createTableView
{
    self.tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-44-20) style:UITableViewStylePlain]autorelease];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 120;
    //关闭横线
    _tableView.separatorStyle = UITableViewCellAccessoryNone;
    _tableView.backgroundColor = [UIColor colorWithRed:255/255.0 green:251/255.0 blue:240/255.0 alpha:1];
    [self.view addSubview:_tableView];
    
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _Arr.count;
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDPersonTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[[XDPersonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
    }
    //cell.textLabel.text = [_Arr[indexPath.row]name];
    cell.Rec = _Arr[indexPath.row];
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XDDetailsViewController *xdd = [[[XDDetailsViewController alloc]init]autorelease];
    
    xdd.ids = [NSString stringWithFormat:@"%@", [self.Arr[indexPath.row] ids]];
    xdd.name = [self.Arr[indexPath.row] name];
    xdd.first_day = [self.Arr[indexPath.row]first_day];
    xdd.day_count = [self.Arr[indexPath.row]day_count];
    xdd.popular_place_str = [self.Arr[indexPath.row]popular_place_str];
    xdd.cover_image = [self.Arr[indexPath.row]cover_image];
    
    [self.navigationController pushViewController:xdd
                                         animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
